package ch1.vola;

/**
 * volatile关键字监听ready属性的变化
 */
public class VolatileCase {
//    private static boolean ready;
    private  static volatile boolean ready;
    private static Integer number;

    private static class PrintThread extends Thread {
        @Override
        public void run() {
            System.out.println("PrintThread is running。。。,ready为" + ready);

            while (!ready) {
            }
            System.out.println("number= " + number);
        }
    }

    public static void main(String[] args) {
        //子线程启动
        new PrintThread().start();

        //休眠1s
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //主线程运行
        System.out.println("main is started!");
        number = 51;
        ready = true;
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("main is ended!");
    }
}
